var editor = new vo.newObj({
	el:'#editor',
	datas:{
		model:{
			title:'请在这里输入标题',
			description:'这里填写简介',
			md:'从这里填写正文',
		},
		name:{
			title:'标题',
			description:'简介',
			md:'正文'
		},
		limit:{
			title:64,
			description:250
		},
		id:id,
		title:'',
		description:'',
		md:'',
		content:'<pre></pre>',
		auto:true
	},
	methods:{
		enter:function(datas){
			var name = target().getAttribute("name");
			var value = datas[name];
			if(value == datas.model[name]){
				this.modify(name,"");
			}
		},
		input:function(datas){
			var t = target();
			var name = t.getAttribute("name");
			this.modify(name,t.value);
		},
		md:function(datas,t){
			if(typeof(t) == 'undefined'){
				t = target();
			}
			var html = t.value;
			this.modify("md",html);
			var reg = new RegExp("\n","g");
			var codeReg = new RegExp("([`]+)([^`]*)\\1","g");
			html = html.replace(codeReg,function($0,$1,$2){
				var count = 0;
				$2 = 0+" "+$2;
				$2 = $2.replace(reg,function($0){
					count++;
					return $0+count+" ";
				});
				return $1+$2+$1;
			});
			//这里做markdown转化
			html = markdown.toHTML(html,'Maruku');
			var content = getElementsByAttr_r(this.el,"name","content")[0];
			content.innerHTML = '<pre>'+html+'</pre>';
			//在这里高亮
			var nodes = content.getElementsByTagName('code');
			if(nodes.length>0){
				for(var i = 0;i<nodes.length;i++){
					if(nodes[i].innerHTML != ""){
						hljs.highlightBlock(nodes[i]);
					}
				}
			}
			this.modify("content",content.innerHTML);
			if(datas.auto){//自动生成简介,从文章中截取250字的简介
				var expw = new RegExp("<p>([^<>]+?)</p>","g");
				var description = "";
				var p = null,tag = 0;
				while((p = expw.exec(content.innerHTML)) != null){
					description += p[1];
					if(description.length >= 250){
						this.modify("description",description.substring(0,250));
						tag = 1;
						break;
					}
				}
				console.log(description);
				if(tag <= 0){
					this.modify("description",description);
				}
				description = null;
			}
			content.scrollTop = content.scrollHeight;
		},
		blur:function(datas){
			var t = target();
			if(t.value == ""){
				console.log("blur");
				var name = target().getAttribute("name");
				this.modify(name,datas.model[name]);	
			}
		},
		turn:function(datas){
			if(datas.auto){
				this.modify("auto",false);
			}else{
				this.modify("auto",true);
			}
		},
		save:function(datas){
			var send = {};
			var wind = getElementsByAttr_r(document, "class", "wind")[0];
			var warn = getElementsByAttr_r(wind, "class", "warn")[0];
			send['title'] = datas['title'];
			send['content'] = datas['content'];
			send['description'] = datas['description'];
			send['md'] = datas['md'];
			if(classifyId != ""){
				send['classify'] = {};
				send['classify']['id'] = classifyId;;
			}
			//这里要对content不闭合标签做下处理,方便以后解析
			 var reg = new RegExp("(<img[\\s\\S]*?[/]{0,1})(>)","g");
			send['content'] = send['content'].replace(reg,function($0,$1,$2){
				$2 = "/"+$2;
				return $1+$2;
			});
			var t = target();
			for(var name in send){
				if(send[name] == "" ||send[name] == datas.model[name]){
					warn.innerHTML = datas.name[name]+"不能为空";
					warn.setAttribute("class","warn error");
					wind.style.display = "inline-block";
					timeout(wind, warn);
					return;
				}else if(send[name].length > datas.limit[name]){
					warn.innerHTML = datas.name[name]+"长度应小于"+datas.limit[name];
					warn.setAttribute("class","warn error");
					wind.style.display = "inline-block";
					timeout(wind, warn);
					return;
				}
			}
			var url = "";
			var method = "";
			if(typeof(id)!='undefined' && id != ""){//当id不为空时,为编辑已有文章
				url = "admin/article/"+id; 
				method = "PUT";
			}else{
				url = "admin/article"; 
				method = "POST";
			}
			if(t.getAttribute("class") == "click-button disabled"){
				return;
			}
			t.setAttribute("class","click-button disabled");
			t.innerHTML = "正在保存...";
			ajax({
				method:method,
				datas:send,
				url:url,
				success:function(resp){
					if(resp.status == "200"){
						warn.innerHTML = "保存文章成功";
						warn.setAttribute("class","warn ok");
						if(typeof(resp.datas)!="undefined"){
							id = resp.datas.id;
						}
						timeout(wind, warn);
					}else{
						warn.innerHTML = resp.status+":"+resp.datas;
						warn.setAttribute("class","warn error");
						timeout(wind, warn);
					}
					t.setAttribute("class","click-button");
					t.innerHTML = "保存文章";
					wind.style.display = "inline-block";
				},
				error:function(){
					warn.innerHTML = "保存文章失败";
					warn.setAttribute("class","warn error");
					wind.style.display = "inline-block";
					timeout(wind,warn);
					t.setAttribute("disabled","false");
				}
			});
		},
		mat:function(){
			mat.el.style.display="block";
			mat.methods.load();
		},
		load:function(datas){
			var model = datas.model;
			if(id != ""){
				ajax({
					method:'GET',
					url:'admin/article/'+id,
					success:function(resp){
						if(resp.status == 200){
							var datas = resp.datas;
							for(var name in model){
								if(typeof(datas[name]) != 'undefined'){
									editor.modify(name,datas[name]);
								}else{
									editor.modify(name,model[name]);
								}
							}
							editor.modify("auto",false);
							editor.methods.md(getElementsByAttr_r(editor.el, "name","md")[0]);
						}else{
							throw new Error(resp.status+":"+resp.datas);
						}
					},
					error:function(){
						throw new Error("加载文章失败");//?未知错误
						for(var name in model){
							editor.modify(name,model[name]);
						}
					}
				});
			}else{
				for(var name in model){
					editor.modify(name,model[name]);
				}
			}
		}
	}
});
editor.methods.load();
